/**
 * https://leetcode.cn/problems/word-pattern/description/
 * 给定一种规律 pattern 和一个字符串 s ，判断 s 是否遵循相同的规律。

这里的 遵循 指完全匹配，例如， pattern 里的每个字母和字符串 s 中的每个非空单词之间存在着双向连接的对应规律。

 

示例1:

输入: pattern = "abba", s = "dog cat cat dog"
输出: true
示例 2:

输入:pattern = "abba", s = "dog cat cat fish"
输出: false
示例 3:

输入: pattern = "aaaa", s = "dog cat cat dog"
输出: false
 * @param {string} pattern
 * @param {string} s
 * @return {boolean}
 */
var wordPattern = function (pattern, s) {
  const arrP = pattern.split("");
  const arrS = s.split(" ");
  if (arrP.length != arrS.length) return false;
  const setP = new Set(arrP);
  const setS = new Set(arrS);
  if (setP.size != setS.size) return false;
  const map = new Map();
  for (let i = 0; i < arrP.length; i++) {
    if (map.has(arrP[i]) && map.get(arrP[i]) != arrS[i]) {
      return false;
    }
    map.set(arrP[i], arrS[i]);
  }
  return true;
};

const pattern = "aaaa",
  s = "dog cat cat fish";
console.log(wordPattern(pattern, s));
